package com.ttctracker.map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.ttctracker.classes.Prediction;
import com.ttctracker.classes.StopItem;

public class StopPredictionHandler extends DefaultHandler {

	private StopItem stop;
	private Prediction predictions;

	public StopPredictionHandler() {
		
		this.predictions = new Prediction(1);
	}

	public StopItem getStop() {
		return stop;
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("predictions")) {
			stop.set_prediction(predictions);
			return;
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (localName.equals("predictions")) {
			stop = new StopItem();
			stop.set_tag(attributes.getValue("stopTag"));
			stop.set_title(attributes.getValue("stopTitle"));
			String noPredictionTextString = attributes
					.getValue("dirTitleBecauseNoPredictions");
			if (noPredictionTextString != null) {
				predictions.add(-1);
				// setTempRouteItem();
			}
			return;
		}
		if (localName.equals("prediction")) {

			String tempValue = attributes.getValue("minutes");
			String prediction = tempValue != null ? tempValue : "-1";
			predictions.add(Integer.parseInt(prediction));
			// setTempRouteItem();

			return;
		}
	}

}
